package cjm.task5.controller;


import cjm.task5.pojo.Record;
import cjm.task5.pojo.User;
import cjm.task5.pojo.UserMessage;
import cjm.task5.service.RecordService;
import cjm.task5.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/usercon")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RecordService recordService;

    // 去到管理员账号管理页面
    @RequestMapping(value = "/AdUser")
    public String AdUser(Model model) {

        List<User> userList = userService.findAllAdUser();
        model.addAttribute("userList", userList);

        return "showAdUser";
    }

    // 去到用户信息页面
    @RequestMapping(value = "/User")
    public String User(Model model) throws ParseException {
        List<UserMessage> userMessages = new ArrayList<UserMessage>();
        List<User> userList = userService.findAllUser();
        for (int i = 0; i < userList.size(); i++) {
            UserMessage userMessage = new UserMessage();

            userMessage.setId(userList.get(i).getId());
            userMessage.setUserid(userList.get(i).getUserid());
            userMessage.setUsername(userList.get(i).getUsername());
            userMessage.setPassword(userList.get(i).getPassword());
            userMessage.setAddress(userList.get(i).getAddress());
            userMessage.setAdmin(userList.get(i).getAdmin());
            userMessage.setIslock(userList.get(i).getIslock());

            //今天是否打卡 1-打卡了 0-未打卡
            Integer flag = recordService.todaySubmit(userList.get(i).getId());
            userMessage.setIsdo(flag);

            //连续打卡天数
            Date date1 = new Date();
            Date date2 = new Date();
            List<String> str = recordService.getAllDateByUid(userList.get(i).getId());
            //System.out.println(str);
            int num = 0;
            if (str.size() > 0) { //说明有打卡记录
                if (str.size() > 1) { //有两个以上的打卡记录
                    num = 1;
                    if(flag == 0){
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String d = sdf.format(date); //今天的日期
                        date1 = sdf.parse(d);
                        date2 = sdf.parse(str.get(0));
                        int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                        if (a != 1) num = 0; //说明昨天有打卡
                        //System.out.println("用户"+userList.get(i).getId()+"进入了判断");
                    }
                    if(num!=0) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        //System.out.println("数组大小: " + str.size());
                        for (int j = 0; j < str.size() - 1; j++) {
                            date1 = format.parse(str.get(j));
                            //System.out.println("date1:" + date1);
                            date2 = format.parse(str.get(j + 1));
                            //System.out.println("date2:" + date2);
                            int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                            //System.out.println("i=" + i + "，相差" + a + "天");
                            if (a == 1) num++;
                            else break;
                        }
                    }
                } else if (flag == 1) num = 1; //说明只有今天打卡了
                else { //只有一个打卡记录，判断是不是昨天！
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String d = sdf.format(date); //今天的日期
                    date1 = sdf.parse(d);
                    date2 = sdf.parse(str.get(0));
                    int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                    if (a == 1) num = 1; //说明昨天有打卡
                }
            }
            userMessage.setDodata(num);
            userMessages.add(userMessage);
        }
        model.addAttribute("userMessages", userMessages);

        return "showUser";
    }

    //修改权限
    @RequestMapping(value = "/updateLock")
    public String updateLock(Model model, @RequestParam("id") Integer id, @RequestParam("isLock") Integer isLock) throws ParseException {
        userService.updateLock(id, isLock);
        User user = userService.findById(id);
        if (user.getAdmin() == 1) {//管理员
            List<User> userList = userService.findAllAdUser();
            model.addAttribute("userList", userList);
            return "showAdUser";
        } else {//用户
            List<UserMessage> userMessages = new ArrayList<UserMessage>();
            List<User> userList = userService.findAllUser();
            for (int i = 0; i < userList.size(); i++) {
                UserMessage userMessage = new UserMessage();

                userMessage.setId(userList.get(i).getId());
                userMessage.setUserid(userList.get(i).getUserid());
                userMessage.setUsername(userList.get(i).getUsername());
                userMessage.setPassword(userList.get(i).getPassword());
                userMessage.setAddress(userList.get(i).getAddress());
                userMessage.setAdmin(userList.get(i).getAdmin());
                userMessage.setIslock(userList.get(i).getIslock());

                //今天是否打卡 1-打卡了 0-未打卡
                Integer flag = recordService.todaySubmit(userList.get(i).getId());
                userMessage.setIsdo(flag);

                //连续打卡天数
                Date date1 = new Date();
                Date date2 = new Date();
                List<String> str = recordService.getAllDateByUid(userList.get(i).getId());
                //System.out.println(str);
                int num = 0;
                if (str.size() > 0) { //说明有打卡记录
                    if (str.size() > 1) { //有两个以上的打卡记录
                        num = 1;
                        if(flag == 0){
                            Date date = new Date();
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            String d = sdf.format(date); //今天的日期
                            date1 = sdf.parse(d);
                            date2 = sdf.parse(str.get(0));
                            int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                            if (a != 1) num = 0; //说明昨天有打卡
                            //System.out.println("用户"+userList.get(i).getId()+"进入了判断");
                        }
                        if(num!=0) {
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                            //System.out.println("数组大小: " + str.size());
                            for (int j = 0; j < str.size() - 1; j++) {
                                date1 = format.parse(str.get(j));
                                //System.out.println("date1:" + date1);
                                date2 = format.parse(str.get(j + 1));
                                //System.out.println("date2:" + date2);
                                int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                                //System.out.println("i=" + i + "，相差" + a + "天");
                                if (a == 1) num++;
                                else break;
                            }
                        }
                    } else if (flag == 1) num = 1; //说明只有今天打卡了
                    else { //只有一个打卡记录，判断是不是昨天！
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String d = sdf.format(date); //今天的日期
                        date1 = sdf.parse(d);
                        date2 = sdf.parse(str.get(0));
                        int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                        if (a == 1) num = 1; //说明昨天有打卡
                    }
                }
                userMessage.setDodata(num);
                userMessages.add(userMessage);
            }
            model.addAttribute("userMessages", userMessages);

            return "showUser";
        }
    }


    //管理员页面升降序(id)
    @RequestMapping(value = "/AdOrderById")
    public String AdOrderById(Model model) {
        List<User> userList = userService.findAllAdUserOrder();
        model.addAttribute("userList", userList);
        return "showAdUser";
    }

    //管理员页面升降序(userid)
    @RequestMapping(value = "/AdOrderByUserId")
    public String AdOrderByUserId(Model model) {
        List<User> userList = userService.findAllAdUserOrderByUserId();
        model.addAttribute("userList", userList);
        return "showAdUser";
    }

    //管理员页面升降序(name)
    @RequestMapping(value = "/AdOrderByName")
    public String AdOrderByName(Model model) {
        List<User> userList = userService.findAllAdUserOrderByName();
        model.addAttribute("userList", userList);
        return "showAdUser";
    }

    //管理员页面升降序(address)
    @RequestMapping(value = "/AdOrderByAddress")
    public String AdOrderByAddress(Model model) {
        List<User> userList = userService.findAllAdUserOrderByAddress();
        model.addAttribute("userList", userList);
        return "showAdUser";
    }


    //用户页面升降序(id)
    @RequestMapping(value = "/UserOrderById")
    public String UserOrderById(Model model) throws ParseException {
        List<User> userList = userService.findAllUserOrder();
        List<UserMessage> userMessages = new ArrayList<UserMessage>();
        for (int i = 0; i < userList.size(); i++) {
            UserMessage userMessage = new UserMessage();
            userMessage.setId(userList.get(i).getId());
            userMessage.setUserid(userList.get(i).getUserid());
            userMessage.setUsername(userList.get(i).getUsername());
            userMessage.setPassword(userList.get(i).getPassword());
            userMessage.setAddress(userList.get(i).getAddress());
            userMessage.setAdmin(userList.get(i).getAdmin());
            userMessage.setIslock(userList.get(i).getIslock());
            //今天是否打卡 1-打卡了 0-未打卡
            Integer flag = recordService.todaySubmit(userList.get(i).getId());
            userMessage.setIsdo(flag);
            //连续打卡天数
            Date date1 = new Date();
            Date date2 = new Date();
            List<String> str = recordService.getAllDateByUid(userList.get(i).getId());
            int num = 0;
            if (str.size() > 0) { //说明有打卡记录
                if (str.size() > 1) { //有两个以上的打卡记录
                    num = 1;
                    if(flag == 0){
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String d = sdf.format(date); //今天的日期
                        date1 = sdf.parse(d);
                        date2 = sdf.parse(str.get(0));
                        int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                        if (a != 1) num = 0; //说明昨天有打卡
                        //System.out.println("用户"+userList.get(i).getId()+"进入了判断");
                    }
                    if(num!=0) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        //System.out.println("数组大小: " + str.size());
                        for (int j = 0; j < str.size() - 1; j++) {
                            date1 = format.parse(str.get(j));
                            //System.out.println("date1:" + date1);
                            date2 = format.parse(str.get(j + 1));
                            //System.out.println("date2:" + date2);
                            int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                            //System.out.println("i=" + i + "，相差" + a + "天");
                            if (a == 1) num++;
                            else break;
                        }
                    }
                } else if (flag == 1) num = 1; //说明只有今天打卡了
                else { //只有一个打卡记录，判断是不是昨天！
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String d = sdf.format(date); //今天的日期
                    date1 = sdf.parse(d);
                    date2 = sdf.parse(str.get(0));
                    int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                    if (a == 1) num = 1; //说明昨天有打卡
                }
            }
            userMessage.setDodata(num);
            userMessages.add(userMessage);

        }
        model.addAttribute("userMessages", userMessages);
        return "showUser";
    }

    //用户页面升降序(userid)
    @RequestMapping(value = "/UserOrderByUserId")
    public String UserOrderByUserId(Model model) throws ParseException {
        List<User> userList = userService.findAllUserOrderByUserId();
        List<UserMessage> userMessages = new ArrayList<UserMessage>();
        for (int i = 0; i < userList.size(); i++) {
            UserMessage userMessage = new UserMessage();
            userMessage.setId(userList.get(i).getId());
            userMessage.setUserid(userList.get(i).getUserid());
            userMessage.setUsername(userList.get(i).getUsername());
            userMessage.setPassword(userList.get(i).getPassword());
            userMessage.setAddress(userList.get(i).getAddress());
            userMessage.setAdmin(userList.get(i).getAdmin());
            userMessage.setIslock(userList.get(i).getIslock());
            //今天是否打卡 1-打卡了 0-未打卡
            Integer flag = recordService.todaySubmit(userList.get(i).getId());
            userMessage.setIsdo(flag);
            //连续打卡天数
            Date date1 = new Date();
            Date date2 = new Date();
            List<String> str = recordService.getAllDateByUid(userList.get(i).getId());
            int num = 0;
            if (str.size() > 0) { //说明有打卡记录
                if (str.size() > 1) { //有两个以上的打卡记录
                    num = 1;
                    if(flag == 0){
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String d = sdf.format(date); //今天的日期
                        date1 = sdf.parse(d);
                        date2 = sdf.parse(str.get(0));
                        int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                        if (a != 1) num = 0; //说明昨天有打卡
                        //System.out.println("用户"+userList.get(i).getId()+"进入了判断");
                    }
                    if(num!=0) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        //System.out.println("数组大小: " + str.size());
                        for (int j = 0; j < str.size() - 1; j++) {
                            date1 = format.parse(str.get(j));
                            //System.out.println("date1:" + date1);
                            date2 = format.parse(str.get(j + 1));
                            //System.out.println("date2:" + date2);
                            int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                            //System.out.println("i=" + i + "，相差" + a + "天");
                            if (a == 1) num++;
                            else break;
                        }
                    }
                } else if (flag == 1) num = 1; //说明只有今天打卡了
                else { //只有一个打卡记录，判断是不是昨天！
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String d = sdf.format(date); //今天的日期
                    date1 = sdf.parse(d);
                    date2 = sdf.parse(str.get(0));
                    int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                    if (a == 1) num = 1; //说明昨天有打卡
                }
            }
            userMessage.setDodata(num);
            userMessages.add(userMessage);

        }
        model.addAttribute("userMessages", userMessages);
        return "showUser";
    }

    //用户页面升降序(name)
    @RequestMapping(value = "/UserOrderByName")
    public String UserOrderByName(Model model) throws ParseException {
        List<User> userList = userService.findAllUserOrderByName();
        List<UserMessage> userMessages = new ArrayList<UserMessage>();
        for (int i = 0; i < userList.size(); i++) {
            UserMessage userMessage = new UserMessage();
            userMessage.setId(userList.get(i).getId());
            userMessage.setUserid(userList.get(i).getUserid());
            userMessage.setUsername(userList.get(i).getUsername());
            userMessage.setPassword(userList.get(i).getPassword());
            userMessage.setAddress(userList.get(i).getAddress());
            userMessage.setAdmin(userList.get(i).getAdmin());
            userMessage.setIslock(userList.get(i).getIslock());
            //今天是否打卡 1-打卡了 0-未打卡
            Integer flag = recordService.todaySubmit(userList.get(i).getId());
            userMessage.setIsdo(flag);
            //连续打卡天数
            Date date1 = new Date();
            Date date2 = new Date();
            List<String> str = recordService.getAllDateByUid(userList.get(i).getId());
            int num = 0;
            if (str.size() > 0) { //说明有打卡记录
                if (str.size() > 1) { //有两个以上的打卡记录
                    num = 1;
                    if(flag == 0){
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String d = sdf.format(date); //今天的日期
                        date1 = sdf.parse(d);
                        date2 = sdf.parse(str.get(0));
                        int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                        if (a != 1) num = 0; //说明昨天有打卡
                        //System.out.println("用户"+userList.get(i).getId()+"进入了判断");
                    }
                    if(num!=0) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        //System.out.println("数组大小: " + str.size());
                        for (int j = 0; j < str.size() - 1; j++) {
                            date1 = format.parse(str.get(j));
                            //System.out.println("date1:" + date1);
                            date2 = format.parse(str.get(j + 1));
                            //System.out.println("date2:" + date2);
                            int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                            //System.out.println("i=" + i + "，相差" + a + "天");
                            if (a == 1) num++;
                            else break;
                        }
                    }
                } else if (flag == 1) num = 1; //说明只有今天打卡了
                else { //只有一个打卡记录，判断是不是昨天！
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String d = sdf.format(date); //今天的日期
                    date1 = sdf.parse(d);
                    date2 = sdf.parse(str.get(0));
                    int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                    if (a == 1) num = 1; //说明昨天有打卡
                }
            }
            userMessage.setDodata(num);
            userMessages.add(userMessage);

        }
        model.addAttribute("userMessages", userMessages);
        return "showUser";
    }

    //用户页面升降序(address)
    @RequestMapping(value = "/UserOrderByAddress")
    public String UserOrderByAddress(Model model) throws ParseException {
        List<User> userList = userService.findAllUserOrderByAddress();
        List<UserMessage> userMessages = new ArrayList<UserMessage>();
        for (int i = 0; i < userList.size(); i++) {
            UserMessage userMessage = new UserMessage();
            userMessage.setId(userList.get(i).getId());
            userMessage.setUserid(userList.get(i).getUserid());
            userMessage.setUsername(userList.get(i).getUsername());
            userMessage.setPassword(userList.get(i).getPassword());
            userMessage.setAddress(userList.get(i).getAddress());
            userMessage.setAdmin(userList.get(i).getAdmin());
            userMessage.setIslock(userList.get(i).getIslock());
            //今天是否打卡 1-打卡了 0-未打卡
            Integer flag = recordService.todaySubmit(userList.get(i).getId());
            userMessage.setIsdo(flag);
            //连续打卡天数
            Date date1 = new Date();
            Date date2 = new Date();
            List<String> str = recordService.getAllDateByUid(userList.get(i).getId());
            int num = 0;
            if (str.size() > 0) { //说明有打卡记录
                if (str.size() > 1) { //有两个以上的打卡记录
                    num = 1;
                    if(flag == 0){
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String d = sdf.format(date); //今天的日期
                        date1 = sdf.parse(d);
                        date2 = sdf.parse(str.get(0));
                        int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                        if (a != 1) num = 0; //说明昨天有打卡
                        //System.out.println("用户"+userList.get(i).getId()+"进入了判断");
                    }
                    if(num!=0) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        //System.out.println("数组大小: " + str.size());
                        for (int j = 0; j < str.size() - 1; j++) {
                            date1 = format.parse(str.get(j));
                            //System.out.println("date1:" + date1);
                            date2 = format.parse(str.get(j + 1));
                            //System.out.println("date2:" + date2);
                            int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                            //System.out.println("i=" + i + "，相差" + a + "天");
                            if (a == 1) num++;
                            else break;
                        }
                    }
                } else if (flag == 1) num = 1; //说明只有今天打卡了
                else { //只有一个打卡记录，判断是不是昨天！
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String d = sdf.format(date); //今天的日期
                    date1 = sdf.parse(d);
                    date2 = sdf.parse(str.get(0));
                    int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                    if (a == 1) num = 1; //说明昨天有打卡
                }
            }
            userMessage.setDodata(num);
            userMessages.add(userMessage);

        }
        model.addAttribute("userMessages", userMessages);
        return "showUser";
    }


    //管理员页面模糊查询
    @RequestMapping(value = "/searchAd")
    public String searchAd(Model model, String searchKeyword) {
        System.out.println(searchKeyword);
        List<User> userList = userService.searchAdUser(searchKeyword);
        model.addAttribute("userList", userList);
        return "showAdUser";
    }

    //用户页面模糊查询
    @RequestMapping(value = "/searchUser")
    public String searchUser(Model model, String searchKeyword) throws ParseException {
        System.out.println(searchKeyword);
        List<UserMessage> userMessages = new ArrayList<UserMessage>();
        List<User> userList = userService.searchUser(searchKeyword);
        for (int i = 0; i < userList.size(); i++) {
            UserMessage userMessage = new UserMessage();
            userMessage.setId(userList.get(i).getId());
            userMessage.setUserid(userList.get(i).getUserid());
            userMessage.setUsername(userList.get(i).getUsername());
            userMessage.setPassword(userList.get(i).getPassword());
            userMessage.setAddress(userList.get(i).getAddress());
            userMessage.setAdmin(userList.get(i).getAdmin());
            userMessage.setIslock(userList.get(i).getIslock());
            //今天是否打卡 1-打卡了 0-未打卡
            Integer flag = recordService.todaySubmit(userList.get(i).getId());
            userMessage.setIsdo(flag);
            //连续打卡天数
            Date date1 = new Date();
            Date date2 = new Date();
            List<String> str = recordService.getAllDateByUid(userList.get(i).getId());
            //System.out.println(str);
            int num = 0;
            if (str.size() > 0) { //说明有打卡记录
                if (str.size() > 1) { //有两个以上的打卡记录
                    num = 1;
                    if(flag == 0){
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String d = sdf.format(date); //今天的日期
                        date1 = sdf.parse(d);
                        date2 = sdf.parse(str.get(0));
                        int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                        if (a != 1) num = 0; //说明昨天有打卡
                        //System.out.println("用户"+userList.get(i).getId()+"进入了判断");
                    }
                    if(num!=0) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        //System.out.println("数组大小: " + str.size());
                        for (int j = 0; j < str.size() - 1; j++) {
                            date1 = format.parse(str.get(j));
                            //System.out.println("date1:" + date1);
                            date2 = format.parse(str.get(j + 1));
                            //System.out.println("date2:" + date2);
                            int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                            //System.out.println("i=" + i + "，相差" + a + "天");
                            if (a == 1) num++;
                            else break;
                        }
                    }
                } else if (flag == 1) num = 1; //说明只有今天打卡了
                else { //只有一个打卡记录，判断是不是昨天！
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String d = sdf.format(date); //今天的日期
                    date1 = sdf.parse(d);
                    date2 = sdf.parse(str.get(0));
                    int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                    if (a == 1) num = 1; //说明昨天有打卡
                }
            }
            userMessage.setDodata(num);
            userMessages.add(userMessage);

        }
        model.addAttribute("userMessages", userMessages);
        return "showUser";
    }

//    //去到今日未打卡用户页面
//    @RequestMapping(value = "/notDoUser")
//    public String notDoUser(Model model) throws ParseException {
//        List<UserMessage> userMessages = new ArrayList<UserMessage>();
//        List<User> userList = userService.findAllUser();
//        for (int i = 0; i < userList.size(); i++) {
//            UserMessage userMessage = new UserMessage();
//
//            userMessage.setId(userList.get(i).getId());
//            userMessage.setUserid(userList.get(i).getUserid());
//            userMessage.setUsername(userList.get(i).getUsername());
//            userMessage.setPassword(userList.get(i).getPassword());
//            userMessage.setAddress(userList.get(i).getAddress());
//            userMessage.setAdmin(userList.get(i).getAdmin());
//            userMessage.setIslock(userList.get(i).getIslock());
//
//            //今天是否打卡 1-打卡了 0-未打卡
//            Integer flag = recordService.todaySubmit(userList.get(i).getId());
//            userMessage.setIsdo(flag);
//
//            //连续打卡天数
//            Date date1 = new Date();
//            Date date2 = new Date();
//            List<String> str = recordService.getAllDateByUid(userList.get(i).getId());
//            System.out.println(str);
//            int num = 0;
//            if (str.size() > 0) { //说明有打卡记录
//                if (str.size() > 1) { //有两个以上的打卡记录
//                    num = 1;
//                    if(flag == 0){
//                        Date date = new Date();
//                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//                        String d = sdf.format(date); //今天的日期
//                        date1 = sdf.parse(d);
//                        date2 = sdf.parse(str.get(0));
//                        int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
//                        if (a != 1) num = 0; //说明昨天有打卡
//                        //System.out.println("用户"+userList.get(i).getId()+"进入了判断");
//                    }
//                    if(num!=0) {
//                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//                        //System.out.println("数组大小: " + str.size());
//                        for (int j = 0; j < str.size() - 1; j++) {
//                            date1 = format.parse(str.get(j));
//                            //System.out.println("date1:" + date1);
//                            date2 = format.parse(str.get(j + 1));
//                            //System.out.println("date2:" + date2);
//                            int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
//                            //System.out.println("i=" + i + "，相差" + a + "天");
//                            if (a == 1) num++;
//                            else break;
//                        }
//                    }
//                } else if (flag == 1) num = 1; //说明只有今天打卡了
//                else { //只有一个打卡记录，判断是不是昨天！
//                    Date date = new Date();
//                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//                    String d = sdf.format(date); //今天的日期
//                    date1 = sdf.parse(d);
//                    date2 = sdf.parse(str.get(0));
//                    int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
//                    if (a == 1) num = 1; //说明昨天有打卡
//                }
//            }
//            userMessage.setDodata(num);
//            if (flag == 0) {
//                userMessages.add(userMessage);
//            }
//        }
//        model.addAttribute("userMessages", userMessages);
//        return "notDoUser";
//    }

    //去到今日未打卡用户页面
    @RequestMapping(value = "/notDoUser")
    public String notDoUser(){
        return "redirect:/usercon/searchNotDoUser";
    }

    private static final int pageSize = 5;
    //未打卡用户页面模糊查询
    @RequestMapping(value = "/searchNotDoUser")
    public ModelAndView searchNotDoUser(
            @RequestParam(value = "searchKeyword",required = false) String searchKeyword,
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum)
            throws ParseException {
        //System.out.println(searchKeyword);
        // 分页操作
        PageHelper.startPage(pageNum,pageSize);
        ModelAndView modelAndView  = new ModelAndView();

        List<UserMessage> userMessages = new ArrayList<UserMessage>();
        List<User> userList = userService.searchUser(searchKeyword);

        // 获取到分页数据
        PageInfo page = new PageInfo(userList);

        for (int i = 0; i < userList.size(); i++) {
            UserMessage userMessage = new UserMessage();
            userMessage.setId(userList.get(i).getId());
            userMessage.setUserid(userList.get(i).getUserid());
            userMessage.setUsername(userList.get(i).getUsername());
            userMessage.setPassword(userList.get(i).getPassword());
            userMessage.setAddress(userList.get(i).getAddress());
            userMessage.setAdmin(userList.get(i).getAdmin());
            userMessage.setIslock(userList.get(i).getIslock());
            //今天是否打卡 1-打卡了 0-未打卡
            Integer flag = recordService.todaySubmit(userList.get(i).getId());
            userMessage.setIsdo(flag);
            //连续打卡天数
            Date date1 = new Date();
            Date date2 = new Date();
            List<String> str = recordService.getAllDateByUid(userList.get(i).getId());
            //System.out.println(str);
            int num = 0;
            if (str.size() > 0) { //说明有打卡记录
                if (str.size() > 1) { //有两个以上的打卡记录
                    num = 1;
                    if(flag == 0){
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String d = sdf.format(date); //今天的日期
                        date1 = sdf.parse(d);
                        date2 = sdf.parse(str.get(0));
                        int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                        if (a != 1) num = 0; //说明昨天有打卡
                        //System.out.println("用户"+userList.get(i).getId()+"进入了判断");
                    }
                    if(num!=0) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        //System.out.println("数组大小: " + str.size());
                        for (int j = 0; j < str.size() - 1; j++) {
                            date1 = format.parse(str.get(j));
                            //System.out.println("date1:" + date1);
                            date2 = format.parse(str.get(j + 1));
                            //System.out.println("date2:" + date2);
                            int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                            //System.out.println("i=" + i + "，相差" + a + "天");
                            if (a == 1) num++;
                            else break;
                        }
                    }
                } else if (flag == 1) num = 1; //说明只有今天打卡了
                else { //只有一个打卡记录，判断是不是昨天！
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String d = sdf.format(date); //今天的日期
                    date1 = sdf.parse(d);
                    date2 = sdf.parse(str.get(0));
                    int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                    if (a == 1) num = 1; //说明昨天有打卡
                }
            }
            userMessage.setDodata(num);
            if (flag == 0) {
                userMessages.add(userMessage);
            }
        }

        //2.将数据存起来
        // 将分页数据存起来
        modelAndView.addObject("page",page);
        modelAndView.addObject("userMessages",userMessages);
        modelAndView.addObject("searchKeyword",searchKeyword);

        //3.跳转到 页面
        modelAndView.setViewName("notDoUser");
        return modelAndView;
    }

    //未打卡用户页面模糊查询
//    @RequestMapping(value = "/searchNotDoUser")
//    public String searchNotDoUser(Model model, String searchKeyword) throws ParseException {
//        //System.out.println(searchKeyword);
//        List<UserMessage> userMessages = new ArrayList<UserMessage>();
//        List<User> userList = userService.searchUser(searchKeyword);
//        for (int i = 0; i < userList.size(); i++) {
//            UserMessage userMessage = new UserMessage();
//            userMessage.setId(userList.get(i).getId());
//            userMessage.setUserid(userList.get(i).getUserid());
//            userMessage.setUsername(userList.get(i).getUsername());
//            userMessage.setPassword(userList.get(i).getPassword());
//            userMessage.setAddress(userList.get(i).getAddress());
//            userMessage.setAdmin(userList.get(i).getAdmin());
//            userMessage.setIslock(userList.get(i).getIslock());
//            //今天是否打卡 1-打卡了 0-未打卡
//            Integer flag = recordService.todaySubmit(userList.get(i).getId());
//            userMessage.setIsdo(flag);
//            //连续打卡天数
//            Date date1 = new Date();
//            Date date2 = new Date();
//            List<String> str = recordService.getAllDateByUid(userList.get(i).getId());
//            System.out.println(str);
//            int num = 0;
//            if (str.size() > 0) { //说明有打卡记录
//                if (str.size() > 1) { //有两个以上的打卡记录
//                    num = 1;
//                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//                    //System.out.println("数组大小: " + str.size());
//                    for (int j = 0; j < str.size() - 1; j++) {
//                        date1 = format.parse(str.get(j));
//                        //System.out.println("date1:" + date1);
//                        date2 = format.parse(str.get(j + 1));
//                        //System.out.println("date2:" + date2);
//                        int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
//                        //System.out.println("i=" + i + "，相差" + a + "天");
//                        if (a == 1) num++;
//                        else break;
//                    }
//                    //如果今天打卡了，要-1
//                    //if (flag == 1) num--;
//                } else if (flag == 1) num = 1; //说明只有今天打卡了
//                else { //只有一个打卡记录，判断是不是昨天！
//                    Date date = new Date();
//                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//                    String d = sdf.format(date); //今天的日期
//                    date1 = sdf.parse(d);
//                    date2 = sdf.parse(str.get(0));
//                    int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
//                    if (a == 1) num = 1; //说明昨天有打卡
//                }
//            }
//            userMessage.setDodata(num);
//            if (flag == 0) {
//                userMessages.add(userMessage);
//            }
//        }
//        model.addAttribute("userMessages", userMessages);
//        return "notDoUser";
//    }


}
